function setModalSize(){
	var documentW = $(window).width();
	var documentH = $(window).height();
	$('#modalW').width(documentW * 0.6);
	$('#modalH').height(documentH * 0.7);	
}

jQuery(document).ready(function() {
	
	initTable();
	
	App.setPage("declare");  //Set current page
	App.init(); //Initialise plugins and elements
	

	//列表查询事件
	$('#search').click(function(){
		$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});
	
	
	//新增
	$('#addDeclare').click(function(){
    	setModalSize();
    	
    	$('#ifModal').attr("src", "add.html");
    	
		$('#modalTile').html("政务项目新增");
		$("#modal").modal('show');
	});
	
	//保存数据事件
	$('#save').click(function(){
		$("#ifModal")[0].contentWindow.save();
	});
	
	
	//清空表单数据事件
	$("#modalDeclear").on("hidden.bs.modal", function() {
      	
	});
	

	//批量删除
	$('#batchDelete').click(function(){
		var data = $('#table').bootstrapTable('getSelections');
		if(data.length <= 0){
			window.wxc.xcConfirm('请勾选要<strong>批量删除</strong>的政务项目数据', window.wxc.xcConfirm.typeEnum.info);
			return;
		}
		var itemnos = "";
		for(var i = 0; i < data.length; i++){
			itemnos += data[i].itemno + ",";
		}
		
		itemnos = itemnos.substring(0, itemnos.length - 1);
		window.wxc.xcConfirm('是否<strong>批量删除</strong>政务项目数据?<br /><span style="color:red;">['+ itemnos +']</span>', window.wxc.xcConfirm.typeEnum.confirm,{
			onOk:function(v){
				$.ajax({ 
					url: basePath() + 'declare/batchDelete.do',
					data: {"itemnos": itemnos},
					dataType: "json",
					success: function(data){	            						
						if(data.result == 1){        							        							
							window.wxc.xcConfirm('删除政务项目['+ data.datas[0].itemno +']成功!', window.wxc.xcConfirm.typeEnum.success,{
								onOk:function(v){
									$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
								}
							});	
						}
			      	}
				});
			}
		});
	});
});		


function initTable(){		
	$('#table').bootstrapTable({
		method: 'post',
	    url: basePath() + 'declare/list.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        ////查询参数,每次调用是会带上这个参数，可自定义                         
        queryParams:function(params) {
            return {
                	pageNum: params.offset / params.limit + 1,
                	pageSize: params.limit,
                	itemno: $('#itemno').val(),
                	itemname: $('#itemname').val(),
                	iflicense: $('#iflicense').val()
                };
        },
	    columns: [{
            field : 'checked',
            checkbox : true

        }, {  
            field : 'number',  
            title : '序号',  
            align : 'center',
            width : '80px',
            formatter : function(value, row, index) {  
// 		                return index + 1;  
                var page = $('#table').bootstrapTable("getPage");  
                return page.pageSize * (page.pageNumber - 1) + index + 1;  
            }  
        }, {
	        field: 'itemno',
	        title: '项目代码'
	    }, {
	        field: 'itemname',
	        title: '项目名称'
	    }, {
	        field: 'itemtype',
	        title: '项目类型',
	        formatter:function(value,row,index){
	        	return  row.itemtypecode + "-" + value;
	        }
	    }, {
	        field: 'iflicense',
	        title: '行政许可项目',
	        formatter:function(value,row,index){
	        	var result = value;
	        	
	        	if(value == "1")
	        		result = "是";
	        	else
        			result = "否";
	        	return result;
	        }
	    }, {
	        field: 'approveflowname',
	        title: '项目流程'
	    },  {
            field: 'operate',
            title: '操作',
            align: 'center',
            width: '400px',
            events: operateEvents,
            formatter: operateFormatter
        } ]
	});
}

function operateFormatter(value, row, index) {
    return [
            '<button type="button" class="Defined btn btn-success  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-glass"></span>流程定义</button>',
            '<button type="button" class="Guide btn btn-inverse  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-pushpin"></span>办事指南</button>',
            '<button type="button" class="Edit btn btn-warning  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-edit"></span>编辑</button>',
            '<button type="button" class="Delete btn btn-danger  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-minus"></span>删除</button>'
    ].join('');
}


window.operateEvents = {
    'click .Defined': function (e, value, row, index) {		//列表操作栏【流程定义】
    	setModalSize();

    	var itemno = row.itemno;
    	$('#ifModal').attr("src", "defined.html?itemno=" + itemno);
    	
		$('#modalTile').html("流程定义");
		$("#modal").modal('show');
    },    
    'click .Guide': function (e, value, row, index) {		//列表操作栏【办事指南】
    	
    	setModalSize();

    	var itemno = row.itemno;
    	var itemname = row.itemname;
    	
    	$('#ifModal').attr("src", "guide.html?itemno=" + itemno);
    	
		$('#modalTile').html("办事指南管理");
		$("#modal").modal('show');
    },    
    'click .Edit': function (e, value, row, index) {		//列表操作栏【编辑】
    	setModalSize();
    	
    	var itemno = row.itemno;
    	
    	$('#ifModal').attr("src", "edit.html?itemno=" + itemno);
    	
		$('#modalTile').html("政务项目修改");
		$("#modal").modal('show');
    	
    },
    'click .Delete': function (e, value, row, index) {		//列表操作栏【删除】

		var itemno = row.itemno;
        

		window.wxc.xcConfirm('是否<strong>删除</strong>政务项目?<br /><span style="color:red;">['+ itemno +']</span>', window.wxc.xcConfirm.typeEnum.info,{
			onOk:function(v){
				$.ajax({ 
					url: projectPath + "declare/delete.do",
					data: {"itemno": itemno},
					dataType: "json",
					success: function(data){	            						
						if(data.result == 1){
				        	$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
						}
			      	}
				});
			}
		});	
    }
 };

function check(data){	
	var result = true;
	

	if($('#tableDictDetail').find('tbody').find('tr').not('.no-records-found').length == 0){
//		$.alert('请增加<span style="color:red;"><strong>字典明细</strong></span>的行数据！');
		
		window.wxc.xcConfirm('请增加<span style="color:red;"><strong>字典明细</strong></span>的行数据！', window.wxc.xcConfirm.typeEnum.error);	
		
		return false;
	}
	
	
	var mustStr = "dictcode,dictname";
	
	var strMsg = "";
	var mustStrs = mustStr.split(',');
	for(var i=0; i<mustStrs.length; i++){
		var obj = $('[name="'+mustStrs[i]+'"]');
		if(obj.val() == ""){
			strMsg += obj.attr("placeholder") + "！<br />";
			
			result = false;
		}
	}
		
	
	if(strMsg != ""){
//		$.alert(strMsg);
		window.wxc.xcConfirm(strMsg, window.wxc.xcConfirm.typeEnum.error);	
	}
	
	return result;
}

function getData(id){
	var allData =  $('#table').bootstrapTable('getData');
	for(var i = 0; i < allData.length; i++){
		if(allData[i].itemno == id)
			return allData[i];
	}
}